import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class DemoJTextField extends JFrame 
{
private JTextField jTFInput1,jTFHasil;
private JButton btnJumlah,btnSave,btnLoad,btnRefresh;
String namaBerkas1 ="Tarea.out";
String namaBerkas2 ="Tarea1.out";

public static void main (String args [])
{
DemoJTextField frame=new DemoJTextField();
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public DemoJTextField()
{
setTitle("Program Luas Bujur Sangkar");
setBackground(Color.blue);
setForeground(Color.black);

JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,2,5,10));
p1.add(new JLabel("Masukkan sisi"));
p1.add(jTFInput1=new JTextField(20));
p1.add(new JLabel("Luas"));
p1.add(jTFHasil=new JTextField(20));
jTFHasil.setEditable(false);

JPanel p2=new JPanel();
p2.setLayout(new GridLayout(3,1,0,10));
p2.add(btnSave=new JButton("Save"));
p2.add(btnLoad=new JButton("Load"));
p2.add(btnRefresh=new JButton("Refresh"));


JPanel p3=new JPanel();
p3.setLayout(new FlowLayout());
p3.add(btnJumlah=new JButton("Hitung Luas"));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.EAST);
getContentPane().add(p3, BorderLayout.SOUTH);

btnJumlah.addActionListener(new DemoJTextField.PenanganTombol());
btnSave.addActionListener(new DemoJTextField.PenanganTombol());
btnLoad.addActionListener(new DemoJTextField.PenanganTombol());
btnRefresh.addActionListener(new DemoJTextField.PenanganTombol());
show ();
}

class PenanganTombol implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String s =e.getActionCommand ();
if (e.getSource()==btnJumlah)
	{
	int bilangan1=(Integer.parseInt(jTFInput1.getText().trim()));
	int hasil=bilangan1*bilangan1;
	
	jTFHasil.setText(String.valueOf(hasil));
	}
	else if (s.equals("Save")) {
	try {
	FileWriter berkasKeluaran1= new FileWriter(namaBerkas1);
	FileWriter berkasKeluaran2 =new FileWriter(namaBerkas2);
	String isiTextArea =jTFInput1.getText();
	String isiHasil =jTFHasil.getText();
	berkasKeluaran1.write(isiTextArea);
	berkasKeluaran2.write(isiHasil);
	berkasKeluaran1.close();
	berkasKeluaran2.close();
	}
	catch (IOException i) {
	System.err.println("Gagal menyimpan berkas");
	System.exit(1);
	}
	jTFInput1.setText("Teks disimpan \n");
	jTFHasil.setText("Teks disimpan \n");
	}
	else if (s.equals("Load")) {
	try {
	FileReader berkasMasukan1 =new FileReader(namaBerkas1);
	BufferedReader streamMasukan1 =new BufferedReader(berkasMasukan1);
	
	FileReader berkasMasukan2 =new FileReader(namaBerkas2);
	BufferedReader streamMasukan2 =new BufferedReader(berkasMasukan2);
	String teks1 ="";
	String teks2 ="";
	while (true) {
	String barisData1 = streamMasukan1.readLine();
	if (barisData1 == null)
	break;
	teks1 =teks1 +barisData1 + '\n'; 
	
	
	while (true) {
	String barisData2 = streamMasukan2.readLine();
	if (barisData2 == null)
	break;
	teks2 =teks2 +barisData2 + '\n'; 
		}
	}
	
	berkasMasukan1.close();
	jTFInput1.setText(teks1);
	berkasMasukan2.close();
	jTFHasil.setText(teks2);
	}
	
	
	catch (IOException i) {
		jTFInput1.setText("Berkas tak dapat dibaca");
		jTFHasil.setText("Berkas tak dapat dibaca");
		}
			}
		else if (s.equals("Refresh")) {
		jTFInput1.setText("");
		jTFHasil.setText("");
		}
			}
				}
					}

		
